#pragma once
#include "Protocol.hpp"


enum
{
    DIV_ZERO = 1,
    MOD_ZERO,
    OTHRE_OPER,
};

class ServerCal
{
public:
    Respond CalculatorHelp(const Request& req)
    {
        Respond resp;
        switch (req._op)
        {
        case '+':
            resp._result = req._x + req._y;
            break;
        case '-':
            resp._result = req._x - req._y;
            break;
        case '*':
            resp._result = req._x * req._y;
            break;
        case '/':
            {
                if (req._y == 0) resp._code = DIV_ZERO;
                else resp._result = req._x / req._y;
                break;
            }

        case '%':
            {
                if (req._y == 0) resp._code = MOD_ZERO;
                else resp._result = req._x % req._y;
                break;
            }
            break;
        default:
            resp._code = OTHRE_OPER;
            break;
        }
        return resp;
    }
    std::string Calculatored(std::string& package)
    {
        // 去报头   
        std::string content;
        int r = Decode(package, &content);
        if (!r) return "";
        // 反序列化
        Request req;
        r = req.Deserialization(content);
        if (!r) return "";
        // 计算
        Respond resp = CalculatorHelp(req);

        // 序列化
        content = "";
        resp.Serialization(&content);
        // 加报头
        std::string str = Encode(content);


        return str;
    }

};